#-*- coding:utf-8 -*-
import time
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

MNIST = input_data.read_data_sets(r"E:\testDir\ml\trainData\mnist",one_hot=True)
# print(MNIST.train.images.shape)
# print(MNIST.train.labels.shape)
# print(MNIST.train.images[1,:])
# print(MNIST.train.labels[1,:])

learning_rate = 0.01
batch_size = 128
n_epoches = 25

X = tf.placeholder(tf.float32,[None,784],"x-input")
Y = tf.placeholder(tf.float32,[None,10],"y-input")

w = tf.Variable(tf.random_normal([784,10],mean=0,stddev=0.01,dtype=tf.float32),name="weight")
b = tf.Variable(tf.zeros([10], dtype=tf.float32, name="bias"))

logits = tf.nn.bias_add(tf.matmul(X,w),b,name="logits")

entropy = tf.nn.softmax_cross_entropy_with_logits(labels=Y,logits=logits)
loss = tf.reduce_mean(entropy)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    n_batches = int(MNIST.train.num_examples/batch_size)
    for i in range(n_epoches):
        for j in range(n_batches):
            X_batch,Y_batch = MNIST.train.next_batch(batch_size)
            _, loss_ = sess.run([optimizer, loss], feed_dict={X: X_batch, Y: Y_batch})
            print("Loss of epochs[{0}] batch[{1}]: {2}".format(i, j, loss_))

    n_batches = int(MNIST.test.num_examples/batch_size)
    total_correct_num = 0
    for i in range(n_batches):
        X_batch, Y_batch = MNIST.test.next_batch(batch_size)
        preds = tf.nn.softmax(tf.nn.bias_add(tf.matmul(X_batch,w),b))
        correct_preds = tf.equal(tf.argmax(preds,1),tf.argmax(Y_batch,1))
        accuracy = tf.reduce_sum(tf.cast(correct_preds,tf.float32))

        total_correct_num += sess.run(accuracy)
    print("Accuracy {0}".format(total_correct_num / MNIST.test.num_examples))






